package com.ljt.base.http

import android.text.TextUtils
import com.ljt.base.utils.LogUtil
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @auther :lujiantao
 * @date :2024/4/27 6:15 PM
 * @desc :自定义联网请求拦截器
 * 1 打印请求地址
 * 2 打印请求参数
 * 3 打印返回值
 * 4 动态添加header
 */
class VicInterceptor:Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {

        //请求模块
        val request = chain.request()
        //接口url
        LogUtil.i("请求url->${request.url()}")
        //请求method
        LogUtil.i("请求method->${request.method()}")
        //请求体打印
        val requestBody:String? = request.body()?.toString()
        if (!TextUtils.isEmpty(requestBody)){
            LogUtil.i("请求body->$requestBody")
        }

        //返回模块
        val response = chain.proceed(request)
        //返回的状态码
        LogUtil.i("返回值code->${response.code()}")
        val responseBody:String? = response.peekBody(1024*1024).string()
        if (!TextUtils.isEmpty(responseBody)){
            LogUtil.i("返回值->$responseBody")
        }
        return response

    }
}